using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class PagingControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// Gets or sets the total number of items.
    /// </summary>
    public int TotalCount
    {
        get
        {
            if (ViewState["TotalCount"] == null)
                return 1;
            else
                return (int)ViewState["TotalCount"];
        }
        set
        {
            ViewState["TotalCount"] = value;
        }
    }

    /// <summary>
    /// Gets or sets the number of items on a page.
    /// </summary>
    public int PageSize
    {
        get
        {
            if (ViewState["PageSize"] == null)
                return 40;
            else
                return (int)ViewState["PageSize"];
        }
        set
        {
            ViewState["PageSize"] = value;
        }
    }

    /// <summary>
    /// Gets the total number of pages.
    /// </summary>
    public int TotalPageCount
    {
        get
        {
            return (TotalCount / PageSize) + 1;
        }
    }

    /// <summary>
    /// Gets if there is a next page.
    /// </summary>
    public bool HasNextPage
    {
        get
        {
            return (TotalPageCount != CurrentPage);
        }
    }

    /// <summary>
    /// Gets if there is a previous page.
    /// </summary>
    public bool HasPreviousPage
    {
        get
        {
            return (CurrentPage != 1);
        }
    }

    /// <summary>
    /// Gets or sets the 1-based page to select.
    /// </summary>
    public int CurrentPage
    {
        get
        {
            if (ViewState["Page"] != null)
                return (int)ViewState["Page"];

            return 1;
        }
        set
        {
            ViewState["Page"] = value;
        }
    }

    /// <summary>
    /// Gets or sets the format with a {0} token for page number used for the URL.
    /// </summary>
    public string UrlFormat
    {
        get
        {
            return ViewState["UrlFormat"] as string;
        }
        set
        {
            ViewState["UrlFormat"] = value;
        }
    }

    /// <summary>
    /// Gets or sets the number of pages to show in the list.
    /// </summary>
    public string PagingatePageCount
    {
        get
        {
            if (ViewState["PaginatePageCount"] == null)
                return 10;

            return (int)ViewState["PagingatePageCount"];
        }
        set
        {
            ViewState["PagingatePageCount"] = value;
        }
    }

    private void DoPages()
    {
        if (UrlFormat == null)
            throw new InvalidOperationException("The UrlFormat for the pager must be specified.");

        int start = Math.Min(TotalPageCount, PagingatePageCount);

        int showPageCount = Math.Min(start + 3, TotalPageCount);
        int[] pages = new int[showPageCount];
        for (int i = 1; i <= start; i++)
            pages[i - 1] = i;

        if (start < TotalPageCount)
        {
            for (int k = TotalPageCount - 2; k <= TotalPageCount; k++)
                pages[10 - (TotalPageCount - k) + 2] = k;
        }

        pagingRepeater.DataSource = pages;
        pagingRepeater.DataBind();
    }

    protected override void OnPreRender(EventArgs e)
    {
        DoPages();
        base.OnPreRender(e);
    }
}
